import { defineStore } from 'pinia'
const path = require('path');
const fs = require('fs');

const settingFilePath = path.join(__lib, 'setting.json');
let setting = {};
try{
	setting = JSON.parse(fs.readFileSync(settingFilePath, 'utf8'));
}catch (e){
}
const state = () => ({
	hga_domain_url: "",
	initHotUpdate: false,
	leagueDisable: [],
	setting: {
		// hga域名
		"hga_url": "https://mos011.com",
		// Acch9kvxu6n hga账号
		"hga_user_name": "",
		// Aa000000 hga密码
		"hga_password": "",
		// 接口刷新间隔
		"hga_api_time": 3,
		// 平博域名
		"ps_url": "https://www.part987.com",
		// yzb20191984 平博账号
		"ps_user_name": "",
		// aa000000 平博密码
		"ps_password": "",
		// 接口刷新间隔
		"ps_api_time": 3,
		// 下注类型
		"bet_type_list": [
			{"type":"concede_points_let", "label":"全场让球", "checked": true},
			{"type":"concede_points_ball", "label":"全场受球", "checked": true},
			{"type":"r_concede_points_let", "label":"上半场让球", "checked": true},
			{"type":"r_concede_points_ball", "label":"上半场受球", "checked": true},
			{"type":"min_big_big", "label":"全场大球", "checked": true},
			{"type":"min_big_min", "label":"全场小球", "checked": true},
			{"type":"r_min_big_big", "label":"上半场大球", "checked": true},
			{"type":"r_min_big_min", "label":"上半场小球", "checked": true},
		],
		// 协议跟单延迟
		"agreement_bet_time_start": 0,
		// 协议跟单延迟
		"agreement_bet_time_end": 5,
		// 接受最低水位
		"agreement_bet_min_odds": 0.2,
		/// 协议金额区域，下注金额
		"agreement_amount_list": [
			{
				'checked': false,
				'range_start': "",
				"range_end": "",
				"amount": ""
			},
			{
				'checked': false,
				'range_start': "",
				"range_end": "",
				"amount": ""
			},
			{
				'checked': false,
				'range_start': "",
				"range_end": "",
				"amount": ""
			}
		],

		// 下注类型服务端分配
		"has_bet_server": true,
		// 下注金额
		"bet_amount": 50,
		// ps 下注金额
		"ps_bet_amount": 10,
		/// 随机数
		"bet_random_max_number": 4,
		/// 随机金额单位
		"bet_random_amount" : 50,
		// 赛前时间
		"game_time_start": 0,
		// 赛前时间
		"game_time_end": 60,

		// 赛中时间
		"game_doing_time_start": 0,
		// 赛中时间
		"game_doing_time_end": 40,

		// 平博最低水位,
		"ps_odds_min": 0.909,
		// 平博最低水位,
		"ps_odds_max": 99,

		// 中单最低水位
		"hga_odds_min": 0.97,
		// 中单最高水位
		"hga_odds_max": 99,

		// 下单间隔秒数
		"bet_interval_time": 3,
		// 失败最多重试次数
		"bet_error_max_count": 3,
		// 下单最低水位
		"bet_min_odds": 0.95,
		// 最多补单次数
		"bet_max_count": 3,
		// 最低限额金额
		"bet_min_amount": 6000,
		// 超过合水值不中
		"bet_count_odds": 1.97,
		// 默认UA
		"bet_user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1",
		// 平博水位差
		"ps_diff_odds_min": 0.06,
		// 平博水位差
		"ps_diff_odds_max": 99,

		/// 滚球配置
		// 中单最低水位
		"doing_hga_odds_min": 0.97,
		// 中单最高水位
		"doing_hga_odds_max": 99,
		// 下单最低水位
		"doing_bet_min_odds": 0.95,
		// 最低限额金额
		"doing_bet_min_amount": 15000,
		// 超过合水值不中
		"doing_bet_count_odds": 1.98,
		// 滚球时间
		"doing_game_time_start": 0,
		// 滚球时间
		"doing_game_time_end": 90,
		// 平博水位差
		"doing_ps_diff_odds_min": 0.06,
		// 平博水位差
		"doing_ps_diff_odds_max": 99,
		// 下注金额
		"doing_bet_amount": 50,
		// ps 下注金额
		"doing_ps_bet_amount": 10,
		// 平博最低水位,
		"doing_ps_odds_min": 0.909,
		// 平博最低水位,
		"doing_ps_odds_max": 99,

		// 允许打赛前比赛
		"open_today_game": true,
		// 允许打滚球比赛
		"open_doing_game": true,
		// 每天打单成功多少单就退出APP，不再打
		"suc_order_max_count": 6,

		// ps赔率加0.3
		// "ps_odds_add": 0.3,
		// hga赔率加0
		// "hga_odds_add": 0,

		// 新打水站点
		"u1368_url": "https://www.u1368.com",
		"u1368_user_name": "",
		"u1368_password": "",
		"u1368_api_time": 3,

		...setting
	},
	hasBetStatus: false,
	/// 关联列表
	nameGuanList: {},
})

export const configStore = defineStore({
	id: 'config_store',
	state,
	actions: {
		updateInitHotUpdate(){
			this.initHotUpdate = true;
		},
		updateHgaDomainUrl(url){
			this.hga_domain_url = url;
		},
		updateSettingConfig(data) {
			this.setting = {...data};
			fs.writeFileSync(settingFilePath, JSON.stringify(data, null, 2));
		},
		updateSettingBetTypeList(bet_type_list){
			if(typeof bet_type_list == 'string') {
				bet_type_list = bet_type_list.split(",");
			}
			this.setting.bet_type_list.forEach((item) => {
				if(bet_type_list.indexOf(item['type']) !== -1) {
					item['checked'] = true;
				}else {
					item['checked'] = false;
				}
			})
		},
		updateNameGuanList(data) {
			this.nameGuanList = {...data};
		},
		updateBetStatus(betStatus) {
			this.hasBetStatus = betStatus;
		},
		updateLeagueDisable(list) {
			this.leagueDisable = list;
		}
	}
})